package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imooc.bo.ModifyUserBO;
import com.imooc.enums.UserRole;
import com.imooc.exception.GraceException;
import com.imooc.mapper.UsersMapper;
import com.imooc.pojo.Users;
import com.imooc.result.ResponseStatusEnum;
import com.imooc.service.UsersService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2022-12-15
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {

    @Override
    @Transactional
    public void modifyUserInfo(ModifyUserBO modifyUserBO) {
        String userId = modifyUserBO.getUserId();
        if(StringUtils.isBlank(userId)){
            GraceException.display(ResponseStatusEnum.USER_INFO_UPDATED_ERROR);
        }
        Users user = new Users();
        user.setId(userId);
        user.setUpdatedTime(LocalDateTime.now());
        BeanUtils.copyProperties(modifyUserBO, user);
        baseMapper.updateById(user);
    }

    @Override
    public Users getUserInfo(String userId) {
        return baseMapper.selectById(userId);
    }

    @Override
    public Long getCountsByCompanyId(String companyId) {
        return baseMapper.selectCount(new LambdaQueryWrapper<Users>().eq(Users::getHrInWhichCompanyId, companyId));
    }

    @Override
    public void updateUserCompanyId(String hrUserId, String realname, String companyId) {
        Users users = new Users();
        users.setId(hrUserId);
        users.setRealName(realname);
        users.setHrInWhichCompanyId(companyId);
        users.setUpdatedTime(LocalDateTime.now());
        baseMapper.updateById(users);
    }

    @Override
    public void changeUserToHR(String userId) {
        Users user = new Users();
        user.setId(userId);
        user.setRole(UserRole.RECRUITER.type);
        user.setUpdatedTime(LocalDateTime.now());
        baseMapper.updateById(user);
    }
}
